package com.yzycoc.coc.gf.service;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yzycoc.coc.gf.config.CocConfig;
import com.yzycoc.coc.gf.constant.ApiUrlConstant;
import com.yzycoc.coc.gf.info.CocVersion;
import com.yzycoc.coc.gf.info.Result;
import com.yzycoc.coc.gf.info.ClanImageInfo;
import com.yzycoc.coc.gf.info.LexiconInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * module-sdk-coc
 *
 * @author Administrator
 * @since 2023/9/7 22:49
 **/
@Slf4j
public class CocService {

    /**
     * 生成部落图片
     *
     * @param tag 标签
     * @return 图片
     */
    public static ClanImageInfo clan(String tag) {
        try {
            String url = CocConfig.API_URL + ApiUrlConstant.IMAGE_CLAN;
            log.debug("clan.http url:{}", url);
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("tag", tag);
            log.debug("clan.http param:{}", paramMap);
            String body = HttpRequest.get(url).header(ApiUrlConstant.TOKEN_AUTHENTICATION, CocConfig.AUTH).timeout(20000).execute().body();
            log.debug("clan.http body:{}", body);
            return JSONObject.parseObject(body, ClanImageInfo.class);
        }catch (Exception e){
            log.error("通讯异常:{}" ,e);
            ClanImageInfo clanImageInfo = new ClanImageInfo();
            clanImageInfo.setSuccess(false);
            clanImageInfo.setResult("服务器通讯异常,请稍后重试!");
            return clanImageInfo;
        }

    }

    /**
     * 生成部落图片
     *
     * @return 图片
     */
    public static ClanImageInfo httpApi(String url, Map<String, Object> paramMap) {
        try {
            log.debug("clan.http url:{} , param:{}", url, paramMap);
            String body = HttpRequest.get(CocConfig.API_URL + url)
                    .form(paramMap)
                    .header(ApiUrlConstant.TOKEN_AUTHENTICATION, CocConfig.AUTH).timeout(20000).execute().body();
            log.debug("clan.http body:{}", body);
            return JSONObject.parseObject(body, ClanImageInfo.class);
        }catch (Exception e){
            log.error("通讯异常:{}" ,e);
            ClanImageInfo clanImageInfo = new ClanImageInfo();
            clanImageInfo.setSuccess(false);
            clanImageInfo.setResult("服务器通讯异常,请稍后重试!");
            return clanImageInfo;
        }

    }


    public static List<LexiconInfo> httpLexicon() {
        try {
            String body = HttpRequest.get(CocConfig.API_URL + ApiUrlConstant.LEXICON)
                    .form("serverType","国服")
                    .header(ApiUrlConstant.TOKEN_AUTHENTICATION, CocConfig.AUTH)
                    .timeout(20000).execute().body();
            log.debug("lexicon.http body:{}", body);
            return JSONArray.parseArray(body, LexiconInfo.class);
        }catch (Exception e){
            log.error("通讯异常:{}" ,e);
        }
        return new ArrayList<>();
    }

    /**
     * 获取lexicon 词库版本
     * @return
     */
    public static CocVersion httpLexiconVersion(String key) {
        try {
            String body = HttpRequest.get(CocConfig.API_URL + ApiUrlConstant.VERSION)
                    .form("key",key)
                    .header(ApiUrlConstant.TOKEN_AUTHENTICATION, CocConfig.AUTH).timeout(20000).execute().body();
            log.debug("version.http body:{}", body);
            return JSONObject.parseObject(body, CocVersion.class);
        }catch (Exception e){
            log.error("通讯异常:{}" ,e);
        }
        return null;
    }


    public static <T> Result<T> httpPost(String url, Map<String, Object> paramMap){
        try {
            log.debug("clan.http url:{} , param:{}", url, paramMap);
            String body = HttpRequest.post(CocConfig.API_URL + url).form(paramMap).header(ApiUrlConstant.TOKEN_AUTHENTICATION, CocConfig.AUTH).execute().body();
            log.debug("clan.http body:{}", body);
            return JSONObject.parseObject(body, Result.class);
        }catch (Exception e){
            e.printStackTrace();
            log.error("通讯异常:{}" ,e);
            Result<T> result = (Result<T>) Result.error("服务器通讯异常,请稍后重试!");
            return result;
        }

    }
}
